package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.enums.BFLX;
import org.footballmanager.pandafootball.system.form.impl.FootCupbfSearchForm;
import org.footballmanager.pandafootball.system.service.FootCupbfService;
import org.footballmanager.pandafootball.system.service.FootCupbmService;
import org.footballmanager.pandafootball.system.service.FootSjService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footCupbfSearchForm")
@RequestMapping("/foot/footCupbf")
public class FootCupbfController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footCupbf";
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

    @Autowired
    private FootCupbfService footCupbfService;
    @Autowired
    private FootCupbmService footCupbmService;
    @Autowired
    private FootUserService footUserService;
    @Autowired
    private FootSjService footSjService;

    /**
     * 初始化
     *
     * @param map                 参数
     * @param footCupbfSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, HttpServletRequest request, @ModelAttribute FootCupbfSearchForm footCupbfSearchForm) {
        map.addAttribute("userList", footUserService.getAllWithoutAdmin(request));
        map.addAttribute("sjList", footSjService.getall());
        FootCupbfSearchForm newFootCupbfSearchForm = new FootCupbfSearchForm();
        if (footCupbfSearchForm != null) {
            newFootCupbfSearchForm = footCupbfSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootCupbfSearchForm);
    }

    /**
     * 杯赛报分列表
     *
     * @param map                 参数
     * @param type                类型
     * @param footCupbfSearchForm 条件参数
     * @return 页面
     */
    @RequestMapping(value = "/search")
    public String search(Model map, Integer type, @ModelAttribute FootCupbfSearchForm footCupbfSearchForm) {
        footCupbfSearchForm.setBflx(type.equals(1) ? BFLX.WINNER : BFLX.ALLIANCE);
        footCupbfService.search(ENTITYNAME, map, footCupbfSearchForm);
        return LISTPAGE;
    }

    /**
     * 杯赛报分删除记录
     *
     * @param id      主键
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footCupbfService.delete(id, request);
        return REDIRECT;
    }

    /**
     * 杯赛报分删除列表
     *
     * @param request             请求
     * @param footCupbfSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute FootCupbfSearchForm footCupbfSearchForm) {
        footCupbfService.deleteAll(footCupbfSearchForm, request);
        return REDIRECT;
    }

    /**
     * 杯赛浏览记录
     *
     * @param map 参数
     * @param id  主键
     * @return 页面
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootCupbfSearchForm footCupbfSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footCupbfSearchForm);
        map.addAttribute(ENTITYNAME, footCupbfService.get(id));
        return VIEWPAGE;
    }

    /**
     * 查看对战页面
     *
     * @param map  参数
     * @param type 类型
     * @return 页面
     */
    @RequestMapping(value = "/bettle", method = RequestMethod.GET)
    public String bettle(Model map, boolean type) {
        footCupbmService.bettle(type, map);
        return "foot/footCupbm/List";
    }

}
